using RealtimeCollabGame.Application.Commands;
using RealtimeCollabGame.Application.Interfaces;
using RealtimeCollabGame.Domain.Entities;
using RealtimeCollabGame.Domain.ValueObjects;

namespace RealtimeCollabGame.Application.Handlers;

public class PaintActionCommandHandler
{
    private readonly IGameRepository _gameRepository;

    public PaintActionCommandHandler(IGameRepository gameRepository)
    {
        _gameRepository = gameRepository;
    }

    public async Task HandleAsync(PaintActionCommand command)
    {
        var game = await _gameRepository.GetByIdAsync(command.GameId);
        if (game == null)
        {
            throw new InvalidOperationException($"游戏 {command.GameId} 不存在");
        }

        var position = Position.Create(command.X, command.Y);
        var brushSize = BrushSize.Create(command.BrushSize);

        game.ProcessPaintAction(command.PlayerId, position, brushSize);
        await _gameRepository.UpdateAsync(game);
    }
} 